package com.seaboxdata.controller;

import com.seaboxdata.entity.CheckRkjzdCondition;
import com.seaboxdata.service.RkjzxxService;
import com.seaboxdata.service.SecurityService;
import com.seaboxdata.util.AssertMapHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-04-12 14:40
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/check")
public class CheckRkjzdController {

    @Autowired
    private RkjzxxService rkjzxxService;

    @Autowired
    private SecurityService securityService;

    @RequestMapping("/rkjzd")
    public Map<String, Object> checkRelation(@RequestBody CheckRkjzdCondition condition) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("retCode", 0);
        resultMap.put("retMessage", "SUCCESS");
        String cardNo = condition.getCardNo();
        String selectKey = condition.getSelectKey();
        String accessKey = condition.getAccessKey();
        AssertMapHandler.isEmptyString(cardNo, resultMap, "查询身份证号码不能为空");
        AssertMapHandler.isEmptyString(selectKey, resultMap, "查询selectKey不能为空");
        AssertMapHandler.isEmptyString(accessKey, resultMap, "查询accessKey不能为空");

        if ((Integer) resultMap.get("retCode") != 0) {
            return resultMap;
        }

        if (!securityService.isSecurity(condition.getSelectKey(), condition.getAccessKey())) {
            resultMap.put("retCode", 1);
            resultMap.put("retMessage", "invalid selectKey or accessKey");
            return resultMap;
        }
        try {
            int count = rkjzxxService.countByCardNo(cardNo);
            if (count > 0) {
                resultMap.put("isDpxqJzrk", true);
            } else {
                resultMap.put("isDpxqJzrk", false);
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("retCode", 1);
            resultMap.put("retMessage", "查询数据库错误,请联系管理员");
        }
        return resultMap;
    }

}
